/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.cell.value.salary.enhance;

import java.util.ArrayList;
import java.util.List;

import com.reportgear.core.data.SalaryHelper;
import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.report.model.calculator.IdParentId;

/**
 * 工资主信息帮助类</br>计算指定类等等哈
 * 
 * @version 1.0 2011-6-16
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 * @deprecated
 */
public enum SalaryMainObjectHelper {
	INSTANCE;

	/**
	 * 根据Id获取本身及所有子对象Id的集合
	 * 
	 * @param id
	 *            Id
	 * @return 集合
	 */
	public List<Integer> getIdAndAllChildOrg(int id) {
		List<IdParentId> idAll = new ArrayList<IdParentId>();
		for (SalaryDataObject sdo : SalaryHelper.INSTANCE.getOrgs()) {
			idAll.add(new IdParentId(sdo.getInt("id"), sdo.getInt("parentId")));
		}

		return this.getIds(id, idAll);
	}

	/**
	 * 根据Id获取本身及所有子对象Id的集合
	 * 
	 * @param id
	 *            Id
	 * @return 集合
	 */
	public List<Integer> getIdAndAllChildCostCenter(int id) {
		List<IdParentId> idAll = new ArrayList<IdParentId>();
		for (SalaryDataObject sdo : SalaryHelper.INSTANCE.getCenters()) {
			idAll.add(new IdParentId(sdo.getInt("id"), sdo.getInt("parentId")));
		}

		return this.getIds(id, idAll);
	}

	private List<Integer> getIds(int id, List<IdParentId> idAll) {
		List<Integer> ids = new ArrayList<Integer>();
		List<Integer> subIds = new ArrayList<Integer>();
		ids.add(id);
		
		for (IdParentId ip : idAll) {
			if (ip.parentId == id) {
				subIds.add(ip.id);
			}
		}

		ids.addAll(subIds);
		for (Integer subId : subIds) {
			ids.addAll(this.getIds(subId, idAll));
		}

		return ids;
	}
}
